<?php

/**
 * ECSHOP 淘宝商品批量导入程序
 * ============================================================================
 * 版权所有 2005-2009 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liubo $
 * $Id: item_extract.php 16881 2009-12-14 09:19:16Z gaoshikao $
*/

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
include_once(ROOT_PATH . 'includes/lib_taoapi.php');

/* 权限检查 */
admin_priv('item_extract');

if (empty($_GET['is_ajax']))
{
    assign_query_info();
    $smarty->assign('ur_here', $_LANG['07_item_extract']);
    $smarty->assign('sid',     $_GET['sid']);
    $smarty->assign('brand_list',   get_brand_list());
    $smarty->display('item_extract.htm');
}
else
{
    include_once(ROOT_PATH . 'includes/cls_json.php');
    $json = new JSON();
    
    /*提取淘宝用户昵称*/
    $sid = intval($_GET['sid']);
    $sql = "SELECT user_id FROM " .$ecs->table('shop'). " WHERE sid = '$sid'";
    $user_id = $db->getOne($sql);

    /* 设置最长执行时间为5分钟 */
    @set_time_limit(300);
    // 默认40张/页
	$page_size = $user_id; 
    $page_no = isset($_GET['page']) ? intval($_GET['page']) : 1;
    $silent = empty($_GET['silent']) ? 0 : 1;

   /* if (isset($_GET['start']))
    {
		//获取淘宝客商品
		$TaoapiItem = taobao_items_get($nick, $page_size, $page_no);
		
		$title = sprintf($_LANG['item_format'], $TaoapiItem['total_results'], $page_size);
		
        $result = array('error' => 0, 'message' => '', 'content' => '', 'done' => 1, 'title' => $title, 'page_size' => $page_size,
            'page' => 1, 'total' => 1, 'silent' => $silent,
            'row' => array('new_page'  => sprintf($_LANG['page_format'], 1),
                           'new_total' => sprintf($_LANG['total_format'], ceil($TaoapiItem['total_results']/$page_size)),
                           'new_time'  => $_LANG['wait'],
                           'cur_id'    => 'time_1'));
        die($json->encode($result));
    }
    else
    {*/
        //$start_time = gmtime(); //开始执行时间
        
        /*获取淘宝客商品*/
       $item_list = array();
        $items = taobao_taobaoke_items_relate_get(497245877);
        if($count <=1){
        	$item_list[] = $items;
        }else $item_list = $items;
        
        foreach ($item_list as $item){
        	$commission=$item['commission'];
        	$commission_num=$item['commission_num'];
        	$commission_rate=$item['commission_rate'];
        	$commission_volume=$item['commission_volume'];
        	$item_location=$item['item_location'];
        	$click_url=$item['click_url'];
        	$num_iid=$item['num_iid'];
        	$title=$item['title'];
        	$nick=$item['nick'];
        	$pic_url=$item['pic_url'];
        	$price=$item['price'];
        	$volume=empty($item['volume'])?0:$item['volume'];
        		
        	$db->query("INSERT INTO ".$ecs->table('item')."(num_iid,title,nick,pic_url,price,click_url,commission,commission_rate,commission_num,commission_volume,item_location,volume)
					values('$num_iid', '$title', '$nick', '$pic_url','$price',$click_url','$commission','$commission_rate',$commission_num','$commission_volume','$item_location','$volume')");
        }
        
        $title = sprintf($_LANG['item_format'], 1, $page_size);
        
        $result = array('error' => 0, 'message' => '', 'content' => '', 'done' => 1, 'title' => $title, 'page_size' => $page_size,
        		'page' => 1, 'total' => 1, 'silent' => $silent,
        		'row' => array('new_page'  => sprintf($_LANG['page_format'], 1),
        				'new_total' => sprintf($_LANG['total_format'], ceil(1/$page_size)),
        				'new_time'  => $_LANG['wait'],
        				'cur_id'    => 'time_1'));
        die($json->encode($result));
        
	   /*获取淘宝客商品*/
	  /*   $TaoapiItem = taobao_items_get($nick, $page_size, $page_no);

		$iids = array();
		$num_iids = array();
		if(is_array($TaoapiItem['items']['item']))
		foreach ($TaoapiItem['items']['item'] as $item){
			$iids[] = $item['iid'];
			$num_iids[] = $item['num_iid'];
			$sql = "SELECT COUNT(num_iid) FROM ".$ecs->table('item')." WHERE num_iid = '$item[num_iid]'";
			$item_count = $db->getOne($sql);
			if(!$item_count){
				$db->query("INSERT INTO ".$ecs->table('item')."(num_iid, iid, title, nick)
					values('$item[num_iid]', '$item[iid]', '$item[title]', '$nick')");
			}
		}

		
		//提取淘宝客商品信息
		$iids = !empty($iids) ? join(',', $iids) : 0;
		$num_iids = !empty($num_iids) ? join(',', $num_iids) : 0;
		
		$items = taobao_taobaoke_items_detail_get_num($num_iids);
		$count_item = $items['total_results'];
		if($count_item <= 1){
			$item_list[] = $items['taobaoke_item_details']['taobaoke_item_detail'];
		}else{
			$item_list = $items['taobaoke_item_details']['taobaoke_item_detail'];
		}
		foreach ($item_list as $key => $item_tmp){
			$click_url=$item_tmp['click_url'];
			$item = $item_tmp['item'];
			
			$detail_url=$item['detail_url'];
			$num=$item['num'];
			$list_time=$item['list_time'];
			$delist_time=$item['delist_time'];
			$num_iid=$item['num_iid'];
			$iid=$item['iid'];
			$desc=$item['desc'];
			$cid=$item['cid'];
			$title=$item['title'];
			$nick=$item['nick'];
			$num_iid=$item['num_iid'];
			$type=$item['type'];
			$pic_url=$item['pic_url'];
			$price=$item['price'];
			$post_fee=$item['post_fee'];
			$volume=empty($item['volume'])?0:$item['volume'];
			
			$sql = "UPDATE ".$ecs->table('item')." SET iid='$iid',click_url='$click_url',detail_url='$detail_url',num='$num',
				list_time='$list_time',delist_time='$delist_time',cat_id='$cid',title='$title',nick='$nick',num_iid='$num_iid',
				type='$type',pic_url='$pic_url',price='$price',post_fee='$post_fee',volume='$volume',last_update='".gmtime()."'
				where iid = '$iid'";
			$db->query($sql);
		}
		
		//转化淘宝客数据
		$item_list = array();
		$items = taobao_taobaoke_items_convert($iids);
		if($count <=1){
			$item_list[] = $items;
		}else $item_list = $items;
		
		foreach ($item_list as $item){
			$commission=$item['commission'];
			$commission_num=$item['commission_num'];
			$commission_rate=$item['commission_rate']; 
			$commission_volume=$item['commission_volume'];
			$item_location=$item['item_location'];
			$click_url=$item['click_url'];
			$iid=$item['iid'];
			
			$sql = "UPDATE ".$ecs->table('item')." SET item_location='$item_location',commission='$commission',commission_num='$commission_num',
				commission_rate='$commission_rate',commission_volume='$commission_volume' WHERE iid = '$iid'";

			$db->query($sql);
		}
		*/

		//更新最后抓取时间
		$db->query("UPDATE ".$ecs->table('shop')." SET last_update = '".gmtime()."' where sid = '$sid'");
		
        /*$result = array('error' => 0, 'message' => '', 'content' => '', 'done' => 2);
        $result['page_size'] = $page_size;
        $result['page']      = 1;
        $result['total']     = 1;
        $result['silent']    = empty($_GET['silent'])    ? 0 : 1;
        
        if ($result['silent'])
        {
            $err_msg = array();
        }*/
        /*------------------------------------------------------ */
        //-- 淘宝商品
        /*------------------------------------------------------ */
    //   $count = $items['total_results'];
        /* 页数在许可范围内 */
       /* if ($result['page'] <= ceil($count / $result['page_size']))
        {
            $result['row']['pre_id'] = 'time_' . $result['page'];
            $result['row']['pre_time'] = ($end_time > $start_time) ? $end_time - $start_time : 1;
            $result['row']['pre_time'] = sprintf($_LANG['time_format'], $result['row']['pre_time']);
            $result['row']['cur_id'] = 'time_' . ($result['page'] + 1);
            $result['page']++; // 新行
            $result['row']['new_page'] = sprintf($_LANG['page_format'], $result['page']);
            $result['row']['new_total'] = sprintf($_LANG['total_format'], ceil($count/$result['page_size']));
            $result['row']['new_time'] = $_LANG['wait'];
            $result['total']++;
      */  //}
       // else
      /*  {
            --$result['total'];
            --$result['page'];
            $result['done'] = 0;
            $result['message'] = $_LANG['done'];
            // 清除缓存 
            clear_cache_files();
            die($json->encode($result));
        }

        if ($result['silent'] && $err_msg)
        {
            $result['content'] = implode('<br />' , $err_msg);
        }
*/
       // die($json->encode($result));
   // }
}

?>